let axios = require("axios")
let cheerio = require("cheerio")

require("./connect.js")

let {
    Bookcover,
} = require("./mod/bookcover.js")
let {
    Chapter
} = require("./mod/chapter.js")


var baseurl = "https://www.qidian.com/finish"
//获取当前页的书籍基本信息
async function getPage(baseurl) {
    let res = await axios.get(baseurl)
    let $ = cheerio.load(res.data)
    // console.log(res)
    let next_url = $(".lbf-pagination-item:nth-last-of-type(1) > a").attr("href") //小说的详情链接
    // 获取所有书籍的li
    $(".all-book-list > div > ul > li").each((index, value) => {
        let book_detail_url = $(value).find(".book-mid-info > h4 > a").attr("href") //小说的详情链接
        let book_coverurl = $(value).find(".book-img-box > a > img").attr("src").trim()
        let book_title = $(value).find(".book-mid-info > h4 > a").text().trim()
        let book_author = $(value).find(".book-mid-info > p.author > a:nth-child(2)").text().trim()
        let book_max_type = $(value).find(".book-mid-info > p.author > a:nth-child(4)").text().trim()
        let book_min_type = $(value).find(".book-mid-info > p.author > a:nth-child(6)").text().trim()
        let book_min_describe = $(value).find(".book-mid-info").html().trim()

        // 保存书的封面信息
        let admin = new Bookcover({
            book_coverurl,
            book_title,
            book_author,
            book_max_type,
            book_min_type,
            book_min_describe
        })
        admin.save(function (err, data) {
            if (err) {
                console.log('保存失败')
            }
        })

        // 获取书的详情页
        setTimeout(() => {
            getbookdetails(book_detail_url, book_title)
        }, index * 2000)

    })

    // console.log(next_url)

    if (next_url.indexOf("javascript:;") != -1) {
        console.log("没有下一页了")
        return 1
    }
    //获取下一页
    next_url = "https:" + next_url

    // 这儿要慎重使用定时器
    getPage(next_url)
}

// 获取书的详情页
async function getbookdetails(url, book_title) {

    url = "https:" + url + "#Catalog"
    let res = await axios.get(url)
    let $ = cheerio.load(res.data)
    let chapter_total = $("#J-catalogCount").text().trim()
    let chapter_grade = $("#j_bookScore").text().trim()

    // console.log($("#j-catalogWrap > div.volume-wrap > div:nth-child(1) > ul > li").length)

    $("#j-catalogWrap > div.volume-wrap > div:nth-child(1) > ul > li").each((index, value) => {

        let chapter_title = $(value).find("a").text().trim()
        let chapter_url = $(value).find("a").attr("href").trim()

        chapter_url = "https:" + chapter_url

        // list=[book_title,chapter_total,chapter_grade,chapter_title,chapter_url]
        // console.log(list)

        setTimeout(() => {
            getchapters(chapter_url, chapter_title, book_title)
        }, index * 2000)
    })

}


// 获取具体的章节内容
async function getchapters(url, chapter_title, book_title) {
    let res = await axios.get(url)
    let $ = cheerio.load(res.data)
    let chapter_numbers = $(".text-info.cf > div > i:nth-child(3) > span").text().trim()
    let chapter_pushtime = $(".text-info.cf > div > i:nth-child(4) > span").text().trim()
    let chapter_content = $(".read-content").html().trim()

    // let list=[chapter_numbers,chapter_pushtime]
    // console.log(list)

    // 保存书的封面信息
    let admin = new Chapter({
        chapter_numbers,
        chapter_pushtime,
        chapter_content,
        chapter_title,
        book_title
    })
    admin.save(function (err, data) {
        if (err) {
            console.log('保存失败')
        }
    })
}

getPage(baseurl)